<#include "/common/defaultEngine.html"/>
<@pageTheme mark=true>
<@header title="公司树" libs=["zTree"] bodyClass="hold-transition box box-main">
	<style>
		body{height:auto;font-family: "Microsoft YaHei";}
		button{font-family: "SimSun","Helvetica Neue",Helvetica,Arial;}
	</style>
</@header>
<@f.hidden  name="treeId" value="${treeId?default(0)}"/>
<@f.hidden  name="treeName" value="${treeName?default('')}"/>
<@f.hidden  name="treeTitle"/>
<@f.hidden  name="parentIds" value="${parentIds?default('')}" />
<@f.hidden  name="type" />
<div class="treeShowHideButton" onclick="$.tree.toggleSearch();">
	<label id="btnShow" title="${i18n("显示搜索")}" style="display:none;">︾</label>
	<label id="btnHide" title="${i18n("隐藏搜索")}">︽</label>
</div>
<div class="treeSearchInput" id="search">
	<label for="keyword">${i18n("关键字")}：</label><input type="text" class="empty" id="keyword" maxlength="50">
	<button class="btn" id="btn" onclick="$.tree.searchNode()"> ${i18n("搜索")} </button>
</div>
<div class="treeExpandCollapse">
	<a href="#" onclick="$.tree.expand()">${i18n("展开")}</a> /
	<a href="#" onclick="$.tree.collapse()">${i18n("折叠")}</a>
</div>
<div id="tree" class="ztree treeselect"></div>
<@footer>
<script>
	$(function() {
		var url = baseURL + "${url}" + <#if correlationId=="">""<#else>"?correlationId=${correlationId}"</#if>;
		<#if checked== "false">
		var options = {
			url: url,
			<#if expandLevel=="true">expandLevel : 2,<#else> expandLevel : 0,</#if>
			onClick : zOnClick,
			onCheck : onCheck,
			onDblClick: onDblClick,
			check: {
				enable: false,
				chkStyle: "radio",
				radioType: "${radioType}"
			}
		};
		<#else>
		var options = {
			url: url,
			_list:$("#treeId").val(),
			<#if expandLevel=="true">expandLevel : 2,<#else> expandLevel : 0,</#if>
			data: {
				simpleData: {
					enable: true,
					idKey: "id",
					pIdKey: "pId",
					rootPId: -1
				},
				key: {
					title: "name",
					name: "title"
				}
			},
			check: {
				enable:true,
				nocheckInherit:true,
				chkboxType:{ "Y" : "s", "N" : "ps" }
			},
			onClick : zOnClick,
			onCheck: onCheck
		};
		</#if>

		$.tree.init(options);
	});
	<#if checked== "false">
	var _p;
	/* 启动回调*/
	function onLoadSuccess(obj,layero, index,_sf){
		this._p = layero;
	}
	function zOnClick(event, treeId, treeNode) {
		<#if isSelectParent== "true">
		if(treeNode.isParent){
			opt.error("父点击不能选择!");
			return;
		}
		</#if>
		$._tree.checkNode(treeNode, !treeNode.checked, null, true);
		$("#parentIds").val($.tree.getParentIds(treeNode));
		var treeId = treeNode.id;
		var treeName = treeNode.name;
		var treeTitle = treeNode.title;
		if(opt.common.isNotEmpty(treeNode.type)){
			$("#type").val(treeNode.type);
		}
		$("#treeId").val(treeId);
		$("#treeName").val(treeName);
		$("#treeTitle").val(treeTitle);
	}

	function onCheck(event, treeId, treeNode) {
		$._tree.selectNode(treeNode);
		$("#parentIds").val($.tree.getParentIds(treeNode));
		var treeId = treeNode.id;
		var treeName = treeNode.name;
		var treeTitle = treeNode.title;
		if(opt.common.isNotEmpty(treeNode.type)){
			$("#type").val(treeNode.type);
		}
		$("#treeId").val(treeId);
		$("#treeName").val(treeName);
		$("#treeTitle").val(treeTitle);
	}

	function submitHandler(index, layero,_sf){
		var sNodes = $._tree.getSelectedNodes();
		if (sNodes.length > 0) {
			<#if isSelectParent== "true">
			if(sNodes[0].isParent){
				opt.error("父点击不能选择!");
				return false;
			}
			</#if>
			if(sNodes[0] != null){
				$("#parentIds").val($.tree.getParentIds(sNodes[0]));
			}
			if(opt.common.isNotEmpty(sNodes[0].type)){
				$("#type").val(sNodes[0].type);
			}
			$("#treeId").val(sNodes[0].id);
			$("#treeName").val(sNodes[0].name);
			$("#treeTitle").val(sNodes[0].title);
		}
		return true;
	}

	/* 双击事件*/
	function onDblClick(event, treeId, treeNode){
		_p.find(".layui-layer-btn0").trigger("click");
	}
	<#else>
	function zOnClick(event, treeId, treeNode) {
		$._tree.checkNode(treeNode, !treeNode.checked, null, true);
	}
	function onCheck(event, treeId, treeNode) {
		var _p = $._tree.getCheckedNodes();
		var ids = "";
		var names = "";
		var titles = "";
		for(var i=0; i<_p.length; i++ ){
			<#if isSelectParent== "true">
			if(_p[i].isParent){
				continue;
			}
			</#if>
			ids += _p[i].id + ",";
			names += _p[i].name + ",";
			titles += _p[i].title + ",";
			$._tree.selectNode(_p);
		}
		$("#treeId").val(ids.substr(0,ids.length-1));
		$("#treeName").val(names.substr(0,names.length-1));
		$("#treeTitle").val(titles.substr(0,titles.length-1));
	}

	function submitHandler(index, layero,_sf){
		var _p = $._tree.getCheckedNodes();
		var ids = "";
		var names = "";
		var titles = "";
		for(var i=0; i<_p.length; i++ ){
			<#if isSelectParent== "true">
			if(_p[i].isParent){
				continue;
			}
			</#if>
			ids += _p[i].id + ",";
			names += _p[i].name + ",";
			titles += _p[i].title + ",";
			$._tree.selectNode(_p);
		}
		$("#treeId").val(ids.substr(0,ids.length-1));
		$("#treeName").val(names.substr(0,names.length-1));
		$("#treeTitle").val(titles.substr(0,titles.length-1));
		return true;
	}
	</#if>
</script>
</@footer>
</@pageTheme>